<?php
/**
 * User: sg
 * Date: 2017/11/28
 * Desc: 股东Model
 */

namespace app\admin\model;
use think\Model;
use app\admin\model\AssetsHelpItem;

class AssetsHelp extends Model{
    protected $connection = 'JS_SERVE';
    protected $table = 'assets_help';
    protected $resultSetType = 'collection';

    //查询单条记录
    public function findData($where,$column='*',$order=[]){
        if($order)
            $data = self::where($where)->field($column)->order($order)->find();
        else
            $data = self::where($where)->field($column)->find();
        $data ? $data = $data->toArray() : '';
        return $data;
    }

    //查询多条记录
    public function selectData($where,$column='*',$order=[]){
        if($order)
            $data = self::where($where)->field($column)->order($order)->select()->toArray();
        else
            $data = self::where($where)->field($column)->select()->toArray();
        return $data;
    }

    //更新记录
    public function editData($where,$data){
        $re = $this->save($data,$where);
        return $re;
    }

    //添加
    public function addData($data){
        $this->data($data);
        $this->isUpdate(false)->save();
        return $this->getLastInsID();
    }

    //删除数据
    public function delData($where){
        $this->destroy($where);
    }

    //筛选数据
    public function searchData($param,$page,$pernum,$order=['Id'=>'desc']){
        if($pernum)
            $data = self::where($param)->order($order)->limit($pernum)->page($page)->select()->toArray();
        else
            $data = self::where($param)->order($order)->select()->toArray();
        return $data;
    }

    //获取总数
    public function getTotalNum($param){
        return self::where($param)->count();
    }

    public function queryData($sql){
        $list = self::query($sql);
        return $list;
    }

    public function assetsHelpItemAdd($uid,$title,$memo,$item)
    {
        $ahid = $this->addData(['uid'=>$uid,'title'=>$title,'memo'=>$memo,'type'=> 8,'addtime'=>date('Y-m-d H:i:s')]);
        $data = [];
        foreach($item as $val)
        {
            $arr = [];
            $arr['ahid'] = $ahid;
            $arr['ahkey'] = $val['ahkey'];
            $arr['ahval'] = $val['ahval'];
            $data[] = $arr;
        }
        if($data)
        {
            $assetsHelpItemMode =  new  AssetsHelpItem();
            $assetsHelpItemMode->insertAll($data);
        }
    }

}